#import "MainView.h"

@implementation MainView
- (id)initWithFrame:(CGRect)rect  controller:(Switcher *)c {
    if ((self == [super initWithFrame:rect]) != nil) {
		controller = c;
        navBar = [self createNavBar:rect];
        [self addSubview:navBar];
		[navBar showLeftButton:@"Prev Page" withStyle:0 rightButton:@"Next Page" withStyle:0];

		CGRect transRect = CGRectMake(rect.origin.x, rect.origin.y + NAV_BAR_HEIGHT, rect.size.width, rect.size.height - NAV_BAR_HEIGHT);
        //transView = [[UITransitionView alloc] initWithFrame:rect];
        transView = [[UITransitionView alloc] initWithFrame:transRect];
        [self addSubview: transView];

		CGRect progressRect = rect;
		progressRect.size.width = 32;
		progressRect.size.height = 32;
		progressView = [[UIProgressHUD alloc] initWithFrame:viewRect];
		[progressView setText:@"switching..."];
		
//        preferencesTable = [[Page1 alloc] initWithFrame:rect];
		HTTPProcessor *processor = [[HTTPProcessor alloc] 
						initWithHost:[[controller getPreferences] objectForKey:@"host"] 
						port:[[controller getPreferences] objectForKey:@"port"]];

		dolog("create pages");
		CGRect pageRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - NAV_BAR_HEIGHT);
        pages[0] = [[Page1 alloc] initWithFrame:pageRect processor:processor];
        pages[1] = [[Page2 alloc] initWithFrame:pageRect processor:processor];
        pages[2] = [[Page3 alloc] initWithFrame:pageRect processor:processor];
        pages[3] = [[Page4 alloc] initWithFrame:pageRect processor:processor];
		dolog("trans");
		
		[transView transition:0 toView:pages[0]];
		[pages[0] reloadData];
		[pages[0] refresh];
		[pages[1] reloadData];
		[pages[2] reloadData];
		currentPageIndex = 0;
		dolog("trans2"); 
//        page2 = [[PrefsView alloc] initWithFrame:rect];
		
//		if ([[controller getPreferences] objectForKey:@"host"] == nil) {
//			[self showPreferences];
//		} else {
//			[self loadCurrentMode];
	}
	
	return self;
}

- (void) loadCurrentMode {
	[self showProgress];
	[controller requestGet];
}

- (void)dealloc {
    [navBar release];
    [navItem release];
	[textView release];
	[progressView release];
	[preferencesTable release];
    [self dealloc];
    [super dealloc];
}

- (UINavigationBar *)createNavBar:(CGRect)rect {
    UINavigationBar *newNav = [[UINavigationBar alloc]
      initWithFrame:
      CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, NAV_BAR_HEIGHT)
    ];
    [newNav setDelegate:self];
    [newNav enableAnimation];
    navItem = [[UINavigationItem alloc] initWithTitle:@"Switcher v2.1"];
    [newNav pushNavigationItem: navItem ];
    return newNav;
}

- (void) showPreferences {
	[navBar showLeftButton:@"Prev" withStyle:0 rightButton:@"Next" withStyle:0];
	dolog("trying to get prefs");
//    [page1 reloadData];
    //[self addSubview:page1];
	preferences = YES;
}

- (void) hidePreferences {
	[preferencesTable setKeyboardVisible:NO];
	[preferencesTable removeFromSuperview];
	[self updateButtons];
	preferences = NO;
}

- (void)navigationBar:(UINavigationBar *)navbar buttonClicked:(int)button {
	dolog("button presses");
	int nextPageIndex;
	int transition;
    if (button == 1) /*Left button*/{
		transition = 2;
		if (currentPageIndex == 0) {
			nextPageIndex = 3;
		} else {
			nextPageIndex = currentPageIndex - 1;
		}
	/*
		if (preferences) {
			[self hidePreferences];
		} else {
			[self showPreferences];
		}
		*/
	} else if (button == 0) /*Right button*/ {
		transition = 1;
		if (currentPageIndex == 3) {
			nextPageIndex = 0;
		} else {
			nextPageIndex = currentPageIndex + 1;
		}
	/*
		if (preferences) {
			[self hidePreferences];
			[controller savePreferences:[preferencesTable getPreferences]];
			if (!currentModeLoaded) {
				[self loadCurrentMode];
			}
		} else {
			[self showProgress];
			[controller requestSet:!mode];
		}
		*/
    }
	dolog("button presses %d %d",currentPageIndex,nextPageIndex);
	[transView transition:transition fromView:pages[currentPageIndex] toView:pages[nextPageIndex]];
	currentPageIndex = nextPageIndex;
}

- (void) updateButtons {
	[navBar showLeftButton:@"Preferences" withStyle:0 rightButton:[self getLabel] withStyle:0];
}

- (NSString *) getLabel {
	if (mode) {
		return @"On";
	}
	return @"Off";
}

- (void) showProgress {
	[self addSubview: progressView];
	[progressView show:YES];
}

- (void) hideProgress {
	[progressView show:NO];
    [progressView removeFromSuperview];
}

- (void) set:(BOOL)_mode {
	mode = _mode;
	[self updateButtons];
	[self log:[@"status " stringByAppendingString:[self getLabel]]];
}

- (void) switched {
	[self set:!mode];
}

- (void) log:(NSString *)message {
	NSString *format = [[@"%H:%M:%S " stringByAppendingString:message] stringByAppendingString:@"\n"];
    NSString *datedMessage = [[NSDate date] 
		descriptionWithCalendarFormat:format 
		timeZone:nil 
		locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
	[textView setText:[datedMessage stringByAppendingString:[textView text]]];
}
@end
